/**   
 * Copyright 2011 COMERE Harold
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package etrl.core;

import java.util.TreeMap;

import etrl.vecmath.Vector3f;
import etrl.vecmath.Vector4f;

/**
 * \class LightStore
 * \brief Singleton class to manage and share lights
 * @author harold
 *
 */
public class LightStore
{
	/**
	 * Singleton instance
	 */
	private static LightStore _instance = null;

	/**
	 * Map mapping light name with light sources
	 */
	private TreeMap <String, LightSource> _lightSources;
	
	/**
	 * Initializes empty light store
	 */
	private LightStore()
	{
		_lightSources = new TreeMap <String, LightSource>();
	}
	
	/**
	 * \brief Gets the singleton instance.
	 * @return Singleton instance.
	 */
	public static LightStore getInstance()
	{
		if(_instance == null)
			_instance = new LightStore();
		
		return _instance;
	}
	
	/**
	 * \brief Creates a new light from the given light name.
	 * @param[in] lightName Requested light name.
	 */
	public void createLight(String lightName)
	{
		LightSource lightSource = _lightSources.get(lightName);
		if(lightSource == null)
			_lightSources.put(lightName, new LightSource());
	}
	
	/**
	 * \brief Sets the position / direction of a given light.
	 * @param[in] lightName Name of the light to modify.
	 * @param[in] positionDirection Position / direction to set.
	 */
	public void setLightPositionDirection(String lightName, Vector3f positionDirection)
	{
		LightSource lightSource = _lightSources.get(lightName);
		if(lightSource != null)
			lightSource.setPositionDirection(positionDirection);
	}
	
	/**
	 * \brief Sets the color of a given light.
	 * @param[in] lightName Name of the light to modify
	 * @param[in] color color to set.
	 */
	public void setLightColor(String lightName, Vector3f color)
	{
		LightSource lightSource = _lightSources.get(lightName);
		if(lightSource != null)
			lightSource.setColor(color);
	}
	
	/**
	 * \brief Gets the position / direction of a given light.
	 * @param[in] lightName Name of the light to check.
	 * @return Position / direction of the requested light if the light exists, else null
	 */
	public Vector4f getLightSourceData(String lightName)
	{
		LightSource lightSource = _lightSources.get(lightName);
		if(lightSource != null)
			return lightSource.getData();
		else
			return null;
	}
	
	/**
	 * \brief Gets the color of a given light.
	 * @param[in] lightName Name of the light to check.
	 * @return Color of the requested light if the light exists, else null.
	 */
	public Vector3f getLightSourceColor(String lightName)
	{
		LightSource lightSource = _lightSources.get(lightName);
		if(lightSource != null)
			return lightSource.getColor();
		else
			return null;		
	}
	 
}
